import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class ClientReadThread extends Thread {
    private Socket socket;
    public Monitor monitor = new Monitor();

    public ClientReadThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        ObjectInputStream ois;
        ExecutorService es = Executors.newCachedThreadPool();
        try {
            while (socket.isConnected()) {
                ois = new ObjectInputStream(socket.getInputStream());
                Object o = ois.readObject();
                Message msgReceived;
                if(o instanceof Message) {
                    msgReceived = (Message)o;
                    es.execute(() -> monitor.startProcess(msgReceived));
                }
            }
        }
        catch (IOException | ClassNotFoundException e) {
            if (e instanceof java.io.EOFException) {
                System.out.println("已断开与服务器的连接");
            } else{
                e.printStackTrace();
            }
        }

    }
}
